package com.youlu.campus.web.learning;

import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.ActivityTaskInfo;
import com.youlu.campus.entity.VO.req.UserDailyLearningCreateVO;
import com.youlu.campus.entity.enums.TaskBizType;
import com.youlu.campus.service.activity.ActivityService;
import com.youlu.campus.service.learning.DailyLearningService;
import com.youlu.campus.web.common.base.RequestContext;
import com.yuelin.infrastructure.quantum.common.QResult;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@Slf4j
@RestController
public class DailyLearningController {
    @Autowired
    private DailyLearningService dailyLearningService;
    @Autowired
    private ActivityService activityService;

    @GetMapping("/mini-app/daily-learning/find-one")
    public QResult getUserDailyLearning(@RequestParam("activityId") String activityId,
                                        @RequestParam("taskBizType") TaskBizType taskBizType) {
        ActivityTaskInfo taskInfo = activityService.findActivityTaskInfo(activityId, taskBizType);
        if (Objects.isNull(taskInfo)) {
            log.error(":>>> 活动没有配置签到学习任务:{}", activityId);
            throw new BusinessException("活动没有配置签到学习任务");
        }
        return QResult.success(dailyLearningService.getUserDailyLearning(activityId, taskInfo, RequestContext.getUserId()));
    }

    /**
     * 活动签到
     *
     * @param req
     * @return
     */
    @PostMapping("/mini-app/daily-learning/sign-up")
    public QResult signUpDailyLearning(@RequestBody UserDailyLearningCreateVO req) {
        ActivityTaskInfo taskInfo = activityService.findActivityTaskInfo(req.getActivityId(), req.getTaskBizType());
        req.setTaskId(taskInfo == null ? null : taskInfo.getId());
        req.setUserId(RequestContext.getUserId());
        req.setUserPO(RequestContext.getCurUser());
        return QResult.success(dailyLearningService.signUp(req));
    }

    /**
     * 获取每日签到海报信息
     *
     * @param id
     * @return
     */
    @PostMapping("/mini-app/daily-learning/play-bill")
    public QResult getPlayBill(@RequestParam("id") String id) {

        return QResult.success(dailyLearningService.getPlayBill(RequestContext.getCurUser(), id, RequestContext.getAppId(), RequestContext.getCurPlatform()));

    }

}
